﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Polls;

namespace NosoiFigthersWeb.PollDesigner {
	public partial class Preview : System.Web.UI.Page {
		protected void Page_Load(object sender, EventArgs e) {
			if (!IsPostBack) {
				Poll poll = Session["Poll"] as Poll;
				
				int qNumber = 1;
				foreach (object obj in poll.Elements) {
					placeHolder.Controls.Add(new Literal() {
						Text = "<p><b>" + (obj is OpenQuestion ? (qNumber++).ToString() + ". " : "")
								+ (obj as Text).Content + "</b></p>"
					});
					
					if (obj is MultipleChoiceQuestion) {
						MultipleChoiceQuestion mcq = obj as MultipleChoiceQuestion;
						placeHolder.Controls.Add(new Literal() {
							Text = "<p><i>question is " + (mcq.MinAnswers > 0 ? "" : "not ") + "obligatory, "
									+ "you may choose several answers, min " + mcq.MinAnswers + ", max "
									+ mcq.MaxAnswers + "</i></p>"
						});
						CheckBoxList cbl = new CheckBoxList() {
							RepeatDirection = (obj as MultipleChoiceQuestion).AnswersArrangement ==
									Arrangement.Horizontal ? RepeatDirection.Horizontal
														   : RepeatDirection.Vertical,
						};
						foreach (string str in (obj as MultipleChoiceQuestion).Answers)
							cbl.Items.Add(new ListItem() {
								Text = str
							});
						placeHolder.Controls.Add(cbl);
					}
					else
					if (obj is SingleChoiceQuestion) {
							SingleChoiceQuestion scq = obj as SingleChoiceQuestion;
							placeHolder.Controls.Add(new Literal() {
								Text = "<p><i>question is " + (scq.Obligatory ? "" : "not ") + "obligatory, "
										+ "you may choose only one answer</i></p>"
							});
							RadioButtonList rbl = new RadioButtonList() {
								RepeatDirection = scq.AnswersArrangement ==
										Arrangement.Horizontal ? RepeatDirection.Horizontal
															   : RepeatDirection.Vertical
							};
							foreach (string str in scq.Answers)
								rbl.Items.Add(new ListItem() {
									Text = str
								});
							placeHolder.Controls.Add(rbl);
					}
					else
					if (obj is NumberQuestion) {
						NumberQuestion nq = obj as NumberQuestion;
						placeHolder.Controls.Add(new Literal() {
							Text = "<p><i>question is " + (nq.Obligatory ? "" : "not ") + "obligatory, "
									+ "you have to type number from " + nq.MinNumber + " to " + nq.MaxNumber
									+ "</i></p>"
						});
						TextBox tb = new TextBox() {
							Width = 100
						};
						placeHolder.Controls.Add(tb);
					}
					else
					if (obj is OpenQuestion) {
						OpenQuestion oq = obj as OpenQuestion;
						placeHolder.Controls.Add(new Literal() {
							Text = "<p><i>question is " + (oq.Obligatory ? "" : "not ") + "obligatory</i></p>"
						});
						TextBox tb = new TextBox() {
							TextMode = TextBoxMode.MultiLine,
							Wrap = true,
							Width = 200,
							Rows = 6
						};
						placeHolder.Controls.Add(tb);
					}
				}
			}
		}
	}
}
